<?php namespace app\api\controller;

use app\common\controller\AuthAndBind;
use think\Controller;
use think\Db;
use alisms\SendSms;
use esign\EsignHandle;

/**
 * api
 */
class DivideIntoConfig extends AuthAndBind{
	
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 分成规则绑定商品
	 */
	public function bindGoods(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'divide_into_config_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$oldDivConf = Db::table("gd_divide_into_config") -> where(['id'=>$in['divide_into_config_id']]) -> find();
				if( empty($oldDivConf) ) returnResults2('-1', '原分成规则数据有误,请联系管理员');
				
				if( isset($in['goods_id']) && !empty($in['goods_id']) ){
					foreach($in['goods_id'] as $k => $v){
						$cW = [];
						$cW[] = ['b.goods_id', 'eq', $v];
						$cW[] = ['b.divide_into_config_id', 'neq', $in['divide_into_config_id']];
						$cW[] = ['a.delivery_type_config_id', 'eq', $oldDivConf['delivery_type_config_id']];
						$c = Db::table("gd_divide_into_config") -> alias('a') -> join('gd_divide_into_config_goods b', 'a.id = b.divide_into_config_id', 'right') -> where($cW) -> find();
						if( !empty($c) ) returnResults2('-1', '操作失败,商品已绑定相同发货方式分成配置');
					}
				}
				
				Db::startTrans();
				$where['divide_into_config_id'] = $in['divide_into_config_id'];
				$old = Db::table("gd_divide_into_config_goods") -> where($where) -> find();
				if( !empty($old) ){
					$delRes = Db::table("gd_divide_into_config_goods") -> where($where) -> delete();
					if( !$delRes ){
						Db::rollback();
						returnResults2('-1', '删除原数据失败,请联系管理员');
					}
				}
				
				$newData = [];
				if( isset($in['goods_id']) && !empty($in['goods_id']) ){
					foreach ($in['goods_id'] as $k => $v) {
						$newData[$k]['goods_id'] = $v;
						$newData[$k]['divide_into_config_id'] = $in['divide_into_config_id'];
					}
					
					$res = Db::table('gd_divide_into_config_goods') -> insertAll($newData);
					if( !$res ){
						Db::rollback();
						returnResults2('-1', '保存数据失败,请联系管理员');
					}
				}
				Db::commit();
				
				$outArr['code'] = '1';
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'分成规则绑定商品',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 详情
	 */
	public function details(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				
				$data = Db::table("gd_divide_into_config") -> where($where) -> find();
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取分成规则配置详情',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 列表
	 */
	public function index(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$num = isset($in['num']) ? $in['num'] : 20;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where = [];
				if( isset($in['create_date']) ) $where[] = ['create_date', 'between', $in['create_date']];
				
				$data = Db::table("gd_divide_into_config") -> where($where) -> order("id desc") -> limit($start, $num) -> select();
				$count = Db::table("gd_divide_into_config") -> where($where) -> count();
				
				if( !empty($data) ){
					$delivery_type_config = Db::table("gd_delivery_type_config") -> select();
					foreach($data as $k => $v){
						$data[$k]['delivery_type_config_name'] = '';
						
						if( !empty($delivery_type_config) ){
							foreach($delivery_type_config as $kk => $vv ){
								if( $v['delivery_type_config_id'] == $vv['id'] ) $data[$k]['delivery_type_config_name'] = $vv['name'];
							}
						}
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取分成规则配置列表',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 编辑
	 */
	public function edit(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				$oldData = Db::table("gd_divide_into_config") -> where($where) -> find();
				if( empty($oldData) ) returnResults(['code'=>'-1', 'msg'=>'原数据有误']);
				
				$save = [];
				foreach ($in as $k => $v) {
					if( !in_array($k, ['id', 'user_id']) ){
						$save[$k] = is_array($v) ? json_encode($v, JSON_UNESCAPED_SLASHES) : strval($v);
					}
				}
				if( !isset($save['value2']) ) $save['value2'] = '0';
				
				if( $save['type'] == 2 ) {
					$nums = 0;
					if( isset($save['value1']) ) $nums = $nums + $save['value1'];
					if( isset($save['value2']) ) $nums = $nums + $save['value2'];
					if( isset($save['value3']) ) $nums = $nums + $save['value3'];
					if( isset($save['value4']) ) $nums = $nums + $save['value4'];
					if( isset($save['value5']) ) $nums = $nums + $save['value5'];
					if( $nums > 100 ) returnResults2('-1', '编辑失败,分成总额不能大于100');
				}
				$save = filterArr($save, array_keys($save), $oldData);
				
				if( isset($save['delivery_type_config_id']) ){
					$oldData = Db::table("gd_divide_into_config_goods") -> where(['divide_into_config_id'=>$oldData['id']]) -> find();
					if( !empty($oldData) ) returnResults(['code'=>'-1', 'msg'=>'该分成配置已绑定商品，无法变更发货方式']);
				}
				
				if( !empty($save) ){
					$re = Db::table("gd_divide_into_config") -> where($where) -> update($save);
					if( !$re ) returnResults2('-1', '编辑失败,请联系管理员');
				}
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'编辑分成规则配置',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 新增
	 */
	public function add(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$newData = [];
				foreach ($in as $k => $v) {
					if( !in_array($k, ['id', 'user_id']) ){
						$newData[$k] = is_array($v) ? json_encode($v, JSON_UNESCAPED_UNICODE) : $v;
					}
					$newData['create_user_id'] = $in['user_id'];
					$newData['create_user_name'] = $userInfo['info']['nickname'];
					$newData['create_user_type'] = $userInfo['info']['type'];
					$newData['create_date'] = date("Y-m-d H:i:s", time());
				}
				if( $newData['type'] == 2 ) {
					$nums = 0;
					if( isset($newData['value1']) ) $nums = $nums + $newData['value1'];
					if( isset($newData['value2']) ) $nums = $nums + $newData['value2'];
					if( isset($newData['value3']) ) $nums = $nums + $newData['value3'];
					if( isset($newData['value4']) ) $nums = $nums + $newData['value4'];
					if( isset($newData['value5']) ) $nums = $nums + $newData['value5'];
					if( $nums > 100 ) returnResults2('-1', '新增失败,分成总额不能大于100');
				}
				$re = Db::table("gd_divide_into_config") -> insertGetId($newData);
				if( !$re ) returnResults2('-1', '新增失败,请联系管理员');
				$outArr['code'] = '1';
				$outArr['info'] = $re;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'新增分成规则配置',
						'business_type'=>1,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
}

?>